<template>
  <div class="container">
    <div class="btnClose">
      <span class="iconfont iconicon-test"></span>
    </div>

    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <!-- 用户名 -->
    <AuthInput
      errMsg="请输入正确的用户名"
      :rule="/^.{5,11}$/"
      placeholder="请输入用户名"
      type="text"
      @setValue="setUsername"
    />
    <!-- 密码 -->
    <AuthInput
      errMsg="密码必须是6-12位数字"
      :rule="/^\d{3,12}$/"
      placeholder="请输入密码"
      type="password"
      @setValue="setPassword"
    />

    <AuthBtn btnText="马上登录" @click.native="login" />
  </div>
</template>

<script>
//1.引入子页面(输入框)
import AuthInput from "../components/AuthInput";
import AuthBtn from "../components/AuthBtn";
export default {
  //2.注册子页面
  components: {
    AuthInput,
    AuthBtn
  },
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    setUsername(newValue) {
      this.username = newValue;
    },
    setPassword(newValue) {
      this.password = newValue;
    },
    login() {
      this.$axios({
        method: "post",
        url: "/login",
        data: {
          username: this.username,
          password: this.password
        }
      }).then(res => {
        console.log(res.data);

        // 解构res.data
        const { message, data } = res.data;

        if (message == "登录成功") {
          this.$toast.success("登录成功");

          // 登录成功后将 token 储存起来
          localStorage.setItem("token", data.token);
          localStorage.setItem("userId",data.user.id);

          //跳转页面
          this.$router.push('/personal')

        } 
      });
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  padding: 20/360 * 100vw;
}
.iconicon-test {
  width: 27/360 * 100vw;
  height: 27/360 * 100vw;
}
.logo {
  text-align: center;
  .iconnew {
    font-size: 127/360 * 100vw;
    color: skyblue;
  }
}
</style>